﻿Imports System.Data.SqlClient
Public Class FrmNhapKiemke
    Dim tbl As DataTable
    Dim dbAdapter As SqlDataAdapter
    Dim cmd As SqlCommand
    Dim tennhap As String
    Private Sub NapPhong()
        cmd = New SqlCommand(" select * from DMPhong", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxPhong.DisplayMember = "Tenphong"
        ComboBoxPhong.ValueMember = "maPhongID"
        ComboBoxPhong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapNgan()
        cmd = New SqlCommand(" select * from DMNgan", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxNgan.DisplayMember = "TenNgan"
        ComboBoxNgan.ValueMember = "maNganID"
        ComboBoxNgan.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKe()
        cmd = New SqlCommand(" select * from DMKe", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxKe.DisplayMember = "TenKe"
        ComboBoxKe.ValueMember = "maKeID"
        ComboBoxKe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKho()
        cmd = New SqlCommand(" select * from DMKho", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxKho.DisplayMember = "TenKho"
        ComboBoxKho.ValueMember = "maKhoID"
        ComboBoxKho.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub Naphoso()
        cmd = New SqlCommand(" select * from DMHosokiemke", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHoso.DisplayMember = "Tenloaihoso"
        ComboBoxHoso.ValueMember = "MaloaiID"
        ComboBoxHoso.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapTaiLieuKK()
        cmd = New SqlCommand(" select * from DMTaiLieuKK", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxTaiLieuKK.DisplayMember = "TenTaiLieuKK"
        ComboBoxTaiLieuKK.ValueMember = "MaTaiLieuKK"
        ComboBoxTaiLieuKK.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDonViHuyen()
        cmd = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHuyen.DisplayMember = "T_huyen"
        ComboBoxHuyen.ValueMember = "maH"
        ComboBoxHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapThoiHanBaoQuan()
        cmd = New SqlCommand(" select * from DMThoiHanBaoQuan", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxThoiHanBQ.DisplayMember = "ThoiHanBaoQuan"
        ComboBoxThoiHanBQ.ValueMember = "MaDMThoiHan"
        ComboBoxThoiHanBQ.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub FrmNhapKiemke_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NapKho()
        NapKe()
        NapNgan()
        NapPhong()
        Naphoso()
        NapTaiLieuKK()
        NapDonViHuyen()
        NapThoiHanBaoQuan()
        dong()

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxHuyen.SelectedIndexChanged, ComboBoxThoiHanBQ.SelectedIndexChanged
        ' load ten xa
        cmd = New SqlCommand(" select * from TenXa where mahuyen = '" & ComboBoxHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(cmd)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxXa.DisplayMember = "TenXa"
        ComboBoxXa.ValueMember = "maxa"
        ComboBoxXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Public Sub NapDuLieuNhapVaodataGird(ByVal dvHC As String)
        DataGridView_hienthi.Rows.Add(DateTimePicker1.Text, TextBoxNamLap.Text, ComboBoxHoso.Text, ComboBoxTaiLieuKK.Text, dvHC, ComboBoxThoiHanBQ.SelectedValue, ComboBoxKho.Text, ComboBoxKe.Text, ComboBoxNgan.Text, ComboBoxPhong.Text)
    End Sub
    Private Sub ButtonLuu_Click(sender As Object, e As EventArgs) Handles ButtonLuu.Click
        If TextBoxNamLap.Text < "0" Or TextBoxNamLap.Text = "" Then
            MessageBox.Show("Năm thực hiện phải lớn hơn không ")
            Return
        Else
            'If txtSoLuongQuyen.Text < "0" Or txtSoLuongQuyen.Text = "" Then
            '    MessageBox.Show("Số lượng phải lớn hơn không ")
            '    Return
            'End If
            If rbTinh.Checked Then
                ThemDuLieuCapTinh()
                NapDuLieuNhapVaodataGird(rbTinh.Text)
            End If
            If rbHuyen.Checked Then
                ThemDuLieuCapHuyen()
                NapDuLieuNhapVaodataGird(rbHuyen.Text)
            End If
            If rbXa.Checked Then
                ThemDuLieuCapXa()
                NapDuLieuNhapVaodataGird(rbXa.Text)
            End If
            dong()
            ob_cnn.Close()
        End If
    End Sub
    Private Sub mo()
        rbTinh.Enabled = True
        rbHuyen.Enabled = True
        rbXa.Enabled = True
        ComboBoxTaiLieuKK.Enabled = True
        ComboBoxHoso.Enabled = True
        TextBoxNamLap.Enabled = True
        DateTimePicker1.Enabled = True
        ComboBoxPhong.Enabled = True
        ComboBoxNgan.Enabled = True
        ComboBoxKe.Enabled = True
        ComboBoxKho.Enabled = True
        ButtonLuu.Enabled = True
    End Sub
    Private Sub dong()
        rbTinh.Enabled = False
        rbHuyen.Enabled = False
        rbXa.Enabled = False
        ComboBoxTaiLieuKK.Enabled = False
        ComboBoxHuyen.Enabled = False
        ComboBoxHoso.Enabled = False
        TextBoxNamLap.Enabled = False
        DateTimePicker1.Enabled = False
        ComboBoxPhong.Enabled = False
        ComboBoxNgan.Enabled = False
        ComboBoxKe.Enabled = False
        ComboBoxKho.Enabled = False
        ButtonLuu.Enabled = False
    End Sub
    Private Function LayIDMax()
        ob_cnn.Open()
        Dim maHoSoID As Int32
        Try
            cmd = New SqlCommand("SELECT MAX(MahosoID) FROM HOSOKIEMKE", ob_cnn)
            maHoSoID = cmd.ExecuteScalar()
        Catch ex As Exception

        End Try
        ob_cnn.Close()
        Return maHoSoID + 1
    End Function
    Private Sub ThemDuLieuCapTinh()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateTimePicker1.Value)
            Dim Sql As String = ""
            Sql = " insert into HOSOKIEMKE(TenQuyen, NgayNhapKho, MaKhoId, MaKeId, MaNganId, MaPhongId, MaTinhTrangId, NguoiNhap, NamThucHien, MaHuyenID, MaxaId, MaLoaiHSID, CapDVHCID, MaDMThoiHanID)" &
                 " values(@tenquyen, @ngaynhapkho, @makhoid, @makeid, @manganid, @maphongid, @matinhtrangid, @nguoinhap, @namthuchien, @huyenID, @xaID, @maloaihosoid, @capdvhcid, @maDMThoiHan)"
            cmd = New SqlCommand(Sql, ob_cnn)
            cmd.Parameters.AddWithValue("@tenquyen", ComboBoxHoso.SelectedValue)
            cmd.Parameters.AddWithValue("@ngaynhapkho", NgayNhap)
            cmd.Parameters.AddWithValue("@makhoid", ComboBoxKho.SelectedValue)
            cmd.Parameters.AddWithValue("@makeid", ComboBoxKe.SelectedValue)
            cmd.Parameters.AddWithValue("@manganid", ComboBoxNgan.SelectedValue)
            cmd.Parameters.AddWithValue("@maphongid", ComboBoxPhong.SelectedValue)
            cmd.Parameters.AddWithValue("@matinhtrangid", "1")
            cmd.Parameters.AddWithValue("@nguoinhap", tennhap)
            cmd.Parameters.AddWithValue("@namthuchien", TextBoxNamLap.Text)
            cmd.Parameters.AddWithValue("@huyenID", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@xaID", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@maloaihosoid", ComboBoxTaiLieuKK.SelectedValue)
            cmd.Parameters.AddWithValue("@mahuyenid", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@maxaid", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@capdvhcid", 1)
            cmd.Parameters.AddWithValue("@maDMThoiHan", ComboBoxThoiHanBQ.SelectedValue)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Thêm thông tin thành công!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub ThemDuLieuCapHuyen()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateTimePicker1.Value)
            Dim Sql As String = ""
            Sql = " insert into HOSOKIEMKE(TenQuyen, NgayNhapKho, MaKhoId, MaKeId, MaNganId, MaPhongId, MaTinhTrangId, NguoiNhap, NamThucHien, MaHuyenID, MaxaId, MaLoaiHSID, CapDVHCID, MaDMThoiHanID)" &
                 " values(@tenquyen, @ngaynhapkho, @makhoid, @makeid, @manganid, @maphongid, @matinhtrangid, @nguoinhap, @namthuchien, @huyenID, @xaID, @maloaihosoid, @capdvhcid, @maDMThoiHan)"
            cmd = New SqlCommand(Sql, ob_cnn)
            cmd.Parameters.AddWithValue("@tenquyen", ComboBoxHoso.SelectedValue)
            cmd.Parameters.AddWithValue("@ngaynhapkho", NgayNhap)
            cmd.Parameters.AddWithValue("@makhoid", ComboBoxKho.SelectedValue)
            cmd.Parameters.AddWithValue("@makeid", ComboBoxKe.SelectedValue)
            cmd.Parameters.AddWithValue("@manganid", ComboBoxNgan.SelectedValue)
            cmd.Parameters.AddWithValue("@maphongid", ComboBoxPhong.SelectedValue)
            cmd.Parameters.AddWithValue("@matinhtrangid", "1")
            cmd.Parameters.AddWithValue("@nguoinhap", tennhap)
            cmd.Parameters.AddWithValue("@namthuchien", TextBoxNamLap.Text)
            cmd.Parameters.AddWithValue("@huyenID", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@xaID", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@maloaihosoid", ComboBoxTaiLieuKK.SelectedValue)
            cmd.Parameters.AddWithValue("@mahuyenid", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@maxaid", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@capdvhcid", 2)
            cmd.Parameters.AddWithValue("@maDMThoiHan", ComboBoxThoiHanBQ.SelectedValue)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Thêm thông tin thành công!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub ThemDuLieuCapXa()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateTimePicker1.Value)
            Dim Sql As String = ""
            Sql = " insert into HOSOKIEMKE(TenQuyen, NgayNhapKho, MaKhoId, MaKeId, MaNganId, MaPhongId, MaTinhTrangId, NguoiNhap, NamThucHien, MaHuyenID, MaxaId, MaLoaiHSID, CapDVHCID, MaDMThoiHanID)" &
                 " values(@tenquyen, @ngaynhapkho, @makhoid, @makeid, @manganid, @maphongid, @matinhtrangid, @nguoinhap, @namthuchien, @huyenID, @xaID, @maloaihosoid, @capdvhcid, @maDMThoiHan)"
            cmd = New SqlCommand(Sql, ob_cnn)
            cmd.Parameters.AddWithValue("@tenquyen", ComboBoxHoso.SelectedValue)
            cmd.Parameters.AddWithValue("@ngaynhapkho", NgayNhap)
            cmd.Parameters.AddWithValue("@makhoid", ComboBoxKho.SelectedValue)
            cmd.Parameters.AddWithValue("@makeid", ComboBoxKe.SelectedValue)
            cmd.Parameters.AddWithValue("@manganid", ComboBoxNgan.SelectedValue)
            cmd.Parameters.AddWithValue("@maphongid", ComboBoxPhong.SelectedValue)
            cmd.Parameters.AddWithValue("@matinhtrangid", "1")
            cmd.Parameters.AddWithValue("@nguoinhap", tennhap)
            cmd.Parameters.AddWithValue("@namthuchien", TextBoxNamLap.Text)
            cmd.Parameters.AddWithValue("@huyenID", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@xaID", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@maloaihosoid", ComboBoxTaiLieuKK.SelectedValue)
            cmd.Parameters.AddWithValue("@mahuyenid", ComboBoxHuyen.SelectedValue)
            cmd.Parameters.AddWithValue("@maxaid", ComboBoxXa.SelectedValue)
            cmd.Parameters.AddWithValue("@capdvhcid", 3)
            cmd.Parameters.AddWithValue("@maDMThoiHan", ComboBoxThoiHanBQ.SelectedValue)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Thêm thông tin thành công!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub ButtonNhap_Click(sender As Object, e As EventArgs) Handles ButtonNhap.Click
        mo()
        TextBoxSoHoSo.Text = LayIDMax()
    End Sub
    Private Sub rbTinh_CheckedChanged(sender As Object, e As EventArgs) Handles rbTinh.CheckedChanged
        If rbTinh.Checked Then
            ComboBoxHuyen.Enabled = False
            ComboBoxXa.Enabled = False
        End If
    End Sub

    Private Sub rbHuyen_CheckedChanged(sender As Object, e As EventArgs) Handles rbHuyen.CheckedChanged
        If rbHuyen.Checked Then
            ComboBoxXa.Enabled = False
            ComboBoxHuyen.Enabled = True
        End If
    End Sub
    Private Sub rbXa_CheckedChanged(sender As Object, e As EventArgs) Handles rbXa.CheckedChanged
        If rbXa.Checked Then
            ComboBoxHuyen.Enabled = True
            ComboBoxXa.Enabled = True
        End If
    End Sub

    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

    Private Sub Label12_Click(sender As Object, e As EventArgs) Handles Label12.Click

    End Sub
End Class